import axios from "axios";
import { getToken, removeToken } from "./token";
import router from "@/router";

const request = axios.create({
  timeout: 60000,
  // baseURL: "http://localhost:8080/api",
  baseURL: "http://geek.itheima.net/v1_0",
});

request.interceptors.request.use(
  (config) => {
    const token = getToken();
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);

request.interceptors.response.use(
  (response) => {
    return response.data;
  },
  (err) => {
    if (err.status === 401) {
      removeToken();
      router.navigate("/login");
      window.location.reload();
    }
    return Promise.reject(err);
  }
);

export default request;
